<template>
    <div class="login-container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form ref="loginFormRef" :model="login_form" :rules="login_rules" label-width="0px" class="login_form">
                <el-form-item prop="username">
                    <el-input
                            placeholder="请输入用户名"
                            prefix-icon="el-icon-user-solid"
                            v-model="login_form.username"
                    ></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                            placeholder="请输入密码"
                            prefix-icon="el-icon-lock"
                            v-model="login_form.password"
                            :type="passwordType"
                    >
                        <i
                                slot="suffix"
                                class="el-icon-view el-input__icon"
                                @click="showPassword"
                        />
                    </el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetForm('loginFormRef')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { setStore, getStore } from '../util/store'

export default {
  name: 'Login',
  data() {
    return {
      passwordType: 'password',
      login_form: {
        username: '',
        password: ''
      },
      login_rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, message: '密码长度最少 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async(valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post('login', this.login_form);
        if (res.meta.status !== 200) return this.$message.error('登录失败');
        this.$message.success('登录成功')
        setStore({ name: 'token', content: res.data.token, type: 'use rToken' })
        this.$router.push('/home')
      })
    },
    showPassword() {
      this.passwordType === '' ? (this.passwordType = 'password') : (this.passwordType = '');
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="less" scoped>
    .login-container{
        background-color: #2b4b6b;
        height: 100%;
    }
    .login_box{
        width: 450px;
        height: 300px;
        background-color: #ffffff;
        border-radius: 4px;
        //绝对定位居中方案
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
     }
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid #eeeeee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 0px #dddddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #ffffff;
        img{
            height: 100%;
            width: 100%;
            border-radius: 50%;
            background-color: #eeeeee;
        }
    }
    .login_form{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }
    .btns{
        display: flex;
        justify-content: flex-end;
    }
</style>
